<?php

use Biz\service\Category as sCategory;
use Biz\service\Goods as sGoods;
use Doraemon\tools\ActionLog;
use Biz\tools\GoodsExposureLog;
use Biz\service\SearchGoods;
use Biz\service\esSearch\Goods as esGoods;

/**
 * @deprecated
 */
class ListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const PAGE_SIZE = 20;

    public function main()
    {
        $categoryId = $this->getRequest()->getQuery('category_id', 0);
        $sortType = $this->getRequest()->getQuery('sort_type', 0);
        $page = $this->getRequest()->getQuery('page', 1);
		$pagesize = $this->getRequest()->getQuery('page_size', self::PAGE_SIZE);
        $provinceId = $this->getRequest()->getQuery('province_id', 0);
        $cityId = $this->getRequest()->getQuery('city_id', 0);

		$coupon_id = $this->getRequest()->getQuery('coupon_id', 0);
        $areaCode = $cityId ? $cityId : $provinceId;
		$goodsParams = ['area_code' => $areaCode];
		if (version_compare($this->appVersion, '3.16.0', "<")) {
		    $goodsParams['not_activity_type'] = [\Biz\pockets\constInterface\ActivityInfo::ACTIVITY_TYPE_PINTUAN];
        }
		if ($coupon_id) {
			$params = $this->getRequest()->getQuery('params', "{}");
			$params = json_decode($params, true);
			$classifyIds = [];
			$minPrice = $maxPrice = 0;
			foreach ($params as $param) {
				if ($param['type'] == 'category') {
					$classifyIds = array_column($param['list'], 'id');
				}
				if ($param['type'] == 'price_range') {
					if (isset($param['list']) && $param['list']) foreach ($param['list'] as $item) {
						if ($item['id'] == 1 && isset($item['title']) && $item['title']) {
							$minPrice = $item['title'];
						}
						if ($item['id'] == 2 && isset($item['title']) && $item['title']) {
							$maxPrice = $item['title'];
						}
					}
				}
			}
			$goodsIds = SearchGoods::searchGoodsByClassifies($classifyIds, $minPrice, $maxPrice, $coupon_id, $sortType, $page);
			// todo 搜索切换es待删除
//			$goodsIds = searchGoods::searchGoodsByClassifies($classifyIds, $minPrice, $maxPrice, $coupon_id, $sortType, $page);
			\Doraemon\tools\Log::debug('goods_seach_by_coupon', [$goodsIds, $coupon_id, $params]);
			$goodsInfos = $goodsIds['rows'] ? sGoods::Goods($goodsIds['rows'], sGoods::FIELDS_GROUP_LIST, $goodsParams) : [];
			$this->setSuccess([
				'list' => array_values($goodsInfos),
				'is_over' => $goodsIds['is_over']
			]);
		} else {

			$cateIdsWithClassifyName = sCategory::getCateIdsByClassifyId($categoryId);
			$where['category_id'] = $cateIdsWithClassifyName['cate_ids'];
			$where['source'] = esGoods::SOURCE_APP;
            $result = SearchGoods::searchEntrance($where, esGoods::SCENES_CATEGORY_ID, $sortType, $page, $pagesize);
			// todo 搜索切换es待删除
//			$goodsIds = searchGoods::searchGoodsByCateIds($cate_ids, $sortType, $page, $pagesize);
			$goodsInfos = sGoods::Goods($result['rows'], sGoods::FIELDS_GROUP_LIST, $goodsParams);
			$this->setSuccess([
				'list' => array_values($goodsInfos),
				'is_over' => $result['is_over']
			]);
			GoodsExposureLog::addLog($this->getRequest(), $goodsInfos,$categoryId);
			$actionLogMap = [
				'default',
				'new',
				'price_asc',
				'price_desc',
			];

			ActionLog::log($this->uid ?: $this->vid, [
				'uid' => $this->uid,
				'part_name' => $cateIdsWithClassifyName['classify_name'],
				'sort_type' => $actionLogMap[$sortType],
				'page' => $page,
			], ActionLog::BIZ_MALL_CATEGORYLIST);

		}


    }
}